--- title: How does the US spending on kids change? author: Xuxin Zhang date: '2020-09-22' slug: post categories: - R project tags: - data visualization - gganimate subtitle: '' summary: '' authors: [] lastmod: '2020-09-22T16:33:37+08:00' featured: no image: caption: '' focal_point: '' preview_only: no projects: [] ---
library(tidyverse)
kids <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')
theme_set(theme_light())
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.0.2
top_states_pub<-kids%>%filter(variable=="pubhealth")%>%
  filter(year ==2012)%>%
  arrange(desc(inf_adj))%>%
  head(3)%>%pull(state)

bottom_states_pub<-kids%>%filter(variable=="pubhealth")%>%
  filter(year ==2012)%>%
  arrange(inf_adj)%>%
  head(3)%>%pull(state)

kids%>%filter(variable=="pubhealth")%>%
  mutate(top_bottom = case_when(state%in%top_states_pub~"Top",
                                state%in%bottom_states_pub~"Bottom",
                                TRUE~"Others"),
         state_plot =case_when(state%in%top_states_pub~state,
                                    state%in%bottom_states_pub~state,
                                    TRUE~""))%>%
  ggplot(aes(x = year, y = inf_adj, group = state))+
  geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
  geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
  geom_text(aes(x = year, y = inf_adj,label = state_plot), 
            family = "Times",
            check_overlap = FALSE)+
  scale_size_manual(values = c(1.5,0.2,1.5))+
  scale_color_manual(values = c("pink","grey","lightgreen"))+
  scale_y_log10()+
  labs(x = "Year",
       y = "Inflation-adjusted spending on public health",
       title = "How the spending on public health (in $1,000s) changes")+
  theme(legend.position = "null",
        plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
  theme_classic()+
  transition_reveal(year)

top_states_unemp<-kids%>%filter(variable=="unemp")%>%
  filter(year ==2012)%>%
  arrange(desc(inf_adj))%>%
  head(3)%>%pull(state)

bottom_states_unemp<-kids%>%filter(variable=="unemp")%>%
  filter(year ==2012)%>%
  arrange(inf_adj)%>%
  head(3)%>%pull(state)

kids%>%filter(variable=="unemp")%>%
  mutate(top_bottom = case_when(state%in%top_states_unemp~"Top",
                                state%in%bottom_states_unemp~"Bottom",
                                TRUE~"Others"),
         state_plot =case_when(state%in%top_states_unemp~state,
                                    state%in%bottom_states_unemp~state,
                                    TRUE~""))%>%
  ggplot(aes(x = year, y = inf_adj,group = state))+
  geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
  geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
  geom_text(aes(x = year, y = inf_adj,label = state_plot), 
            family = "Times",
            check_overlap = FALSE)+
  scale_size_manual(values = c(1.5,0.2,1.5))+
  scale_color_manual(values = c("pink","grey","lightgreen"))+
  scale_y_log10()+
  labs(x = "Year",
       y = "Inflation-adjusted spending on unemployment benefits",
       title = "How the Unemployment benefits (in $1,000s) changes")+
  theme(legend.position = "null",
        plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
  theme_classic()+
  transition_reveal(year)

top_states_pk<-kids%>%filter(variable=="PK12ed")%>%
  filter(year ==2012)%>%
  arrange(desc(inf_adj))%>%
  head(3)%>%pull(state)

bottom_states_pk<-kids%>%filter(variable=="PK12ed")%>%
  filter(year ==2012)%>%
  arrange(inf_adj)%>%
  head(3)%>%pull(state)

kids%>%filter(variable=="pubhealth")%>%
  mutate(top_bottom = case_when(state%in%top_states_pk~"Top",
                                state%in%bottom_states_pk~"Bottom",
                                TRUE~"Others"),
         state_plot =case_when(state%in%top_states_pk~state,
                                    state%in%bottom_states_pk~state,
                                    TRUE~""))%>%
  ggplot(aes(x = year, y = inf_adj, group = state))+
  geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
  geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
  geom_text(aes(x = year, y = inf_adj,label = state_plot), 
            family = "Times",
            check_overlap = FALSE)+
  scale_size_manual(values = c(1.5,0.2,1.5))+
  scale_color_manual(values = c("pink","grey","lightgreen"))+
  scale_y_log10()+
  labs(x = "Year",
       y = "Inflation-adjusted spending on K12 education",
       title = "How the spending on K12 education (in $1,000s) changes")+
  theme(legend.position = "null",
        plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
  theme_classic()+
  transition_reveal(year)

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
state_year_loc <- kids%>%filter(variable=="PK12ed")%>%
  mutate(year2 = as.factor(year))%>%
  mutate(state = str_to_lower(state))%>%
  group_by(state, year2)%>%summarise(inf_adj= mean(inf_adj))%>%
  right_join(map_data("state"),by =c("state"="region"))
## `summarise()` regrouping output by 'state' (override with `.groups` argument)
ggplotly(state_year_loc%>%mutate(year = as.numeric(year2)) %>%
  ggplot(aes(x = long, y = lat, group = group))+
  geom_polygon(aes(fill = inf_adj))+
  scale_fill_gradient(low = "yellow", high = "orangered")+
  facet_wrap(~year2)+
  theme(plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
  theme_void()+
  labs(title = "The heatmap of spending on K12 education from 1997 to 2016",
       fill = "Spending"))
library(ggrepel)
k12_data <- kids%>%filter(variable=="PK12ed")%>%
  filter(year%in%c(1997,2016))%>%mutate(year = as.character(year))%>%
  select(-raw,-inf_adj_perchild)%>%
  pivot_wider(names_from = year, values_from = inf_adj)

names(k12_data)<-c("state","variable","data1997","data2016")
  
k12_data%>%mutate(pct_change = data2016/data1997)%>%
  ggplot(aes(x = data1997, y = pct_change, group = state))+geom_point()+
  scale_x_log10()+expand_limits(x = 500000, y = 0)+
    geom_text_repel(aes(label = state), size = 3)

ggplotly(k12_data%>%mutate(change = data2016-data1997)%>%
  select(-variable)%>%
  pivot_longer(data1997:data2016,names_to ="year", values_to = "adj_inf")%>%
  ggplot(aes(x = adj_inf, y = reorder(state,change)))+
  geom_point(aes(color = year),size = 3)+geom_line(size = 2,alpha = 0.1)+theme_classic()+
  labs(x = "Government spending adjusted by inflation",
       y = "State ordered by change in spending",
       title = "How the government spending on K12 education changes from 1997 to 2016")+
  scale_color_brewer(palette = "Paired")+
  scale_x_continuous(labels = scales::comma)+
  theme(plot.title = element_text(face = "bold", size = 14, margin = margin(50,0,5,0))))
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
kids_clean<-kids%>%filter(variable == "PK12ed")%>%
  transmute(state, year, inf_adj_perchild=round(inf_adj_perchild,3)*1000)%>%
  group_by(year)%>%
  arrange(desc(inf_adj_perchild))%>%
  mutate(ranking = 1:n())%>%filter(ranking <=15)

kids_clean%>%ggplot(aes(x = ranking, y = inf_adj_perchild, fill = state))+
  geom_col(show.legend = FALSE, alpha = 0.6)+
  geom_text(aes(y = 0, label = state), family = "Times",hjust=0)+
  geom_text(aes(y = inf_adj_perchild, label = as.character(dollar(inf_adj_perchild))),hjust = -0.1)+
  geom_text(aes(label = as.character(year)),x = -15, y = 18000, size = 16, family = "Times")+
  coord_flip(clip = "off")+scale_x_reverse()+ylim(0,20000) +
  coord_flip(clip = "off", expand = TRUE) +
  scale_x_reverse() +
  ylim(-10,19000) + 
  theme_minimal() +
  theme(legend.position = "none",
        plot.margin = margin(30, 30, 30, 30),
        plot.title = element_text(size = 20, family = "Times", face = "bold", margin = margin(20,0,5,0)),
        plot.subtitle = element_text(size = 14, family = "Times", margin = margin(0,0,15,0)),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_text(size = 12, family = "Times"))  +
  labs(y = "Spending per individual",
       title = "US Spending on K12 Education",
       subtitle = "Top 15 States in...") +
  transition_states(year)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.